/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package mvc.model;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;


/**
 *
 * @author MAN
 */
public class Usuario_Dao extends Model implements DaoGenerico {


     public boolean existe(Object o) throws SQLException{
             Usuario per=(Usuario)o;
             Connection conex=getCon();
		boolean sw=true;
		PreparedStatement pstmt=null;
		ResultSet rs=null;

		try{
			pstmt=conex.prepareStatement("SELECT COUNT(*) FROM usuario WHERE nombre=? AND clave=?");
			pstmt.setString(1, per.getNombreUsuario());
                        pstmt.setString(2, per.getPassword());
			rs=pstmt.executeQuery();
			if(rs.next())
				if(rs.getString(1).equals("0"))
					sw=false;
		}finally{
			if(rs!=null)
				rs.close();

			if(pstmt!=null)
				pstmt.close();
		}

		return sw;
	}



      public Object load(ResultSet rs) throws SQLException {

        Usuario u = new Usuario();
        u.setNombreUsuario(rs.getString(1));
        u.setPassword(rs.getString(2));
        u.setTipo(rs.getString(3));


        return u;
    }


      public Object extrae (Object id) throws SQLException{
          String id1=(String)id;
                    Connection conex=getCon();

                if (!isConnect())
			throw new SQLException("no hay conexcion");
                Usuario u= new Usuario();
		PreparedStatement pstmt=null;
		ResultSet rs=null;

		try{
			pstmt=conex.prepareStatement("SELECT * FROM usuario WHERE nombre=?");
			pstmt.setString(1,id1);
			rs=pstmt.executeQuery();
			if(rs.next()){
			  u=(Usuario)load(rs);

                        }else{

                        }
		}finally{
			if(rs!=null)
				rs.close();

			if(pstmt!=null)
				pstmt.close();
		}

		return u;
     }


        public void crear(Object o)throws SQLException{
         Usuario u=(Usuario)o;
             Connection conex=getCon();

                if(existe(u))
                    throw new SQLException("Ya existe esta usuario");
		PreparedStatement pstmt=null;

		try{

			pstmt=conex.prepareStatement("INSERT INTO usuario VALUES (?,?,?,?)");


			pstmt.setString(1,u.getNombreUsuario());
			pstmt.setString(2,u.getPassword());
			pstmt.setString(3,u.getTipo());

			pstmt.executeUpdate();

		}finally{
			if(pstmt!=null){
				pstmt.close();
				pstmt=null;
			}
		}

  }



       public List listar() throws SQLException {
              Connection conex=getCon();
        if (!isConnect())
		throw new SQLException("no hay conexcion");

        PreparedStatement ps = null;
        ResultSet rs = null;
        Usuario u = null;
        List lista = new LinkedList();
        try {
            ps =(PreparedStatement)  conex.prepareStatement("select * from personas ");
            rs = ps.executeQuery();

            while (rs.next()) {
                u =(Usuario) load(rs);
                lista.add(u);
            }

        } finally {
            if (rs != null) {
                rs.close();
            }
            if (ps != null) {
                ps.close();
            }
        }
        return lista;
    }

       public String getTipoUsuario(Usuario per) throws SQLException{

		String tipo="";
		PreparedStatement pstmt=null;
		ResultSet rs=null;

		try{
			pstmt=getCon().prepareStatement("SELECT tipo FROM Usuario WHERE nombre=? AND clave=?");
			pstmt.setString(1, per.getNombreUsuario());
                        pstmt.setString(2, per.getPassword());
			rs=pstmt.executeQuery();
			if(rs.next())
				tipo=rs.getString(1);

		}finally{
			if(rs!=null)
				rs.close();

			if(pstmt!=null)
				pstmt.close();
		}

		return tipo;
	}

    public Object extrae2(Object id) throws SQLException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public List listar(Object o) throws SQLException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

   

      public void Update(Object o) throws SQLException {
        throw new UnsupportedOperationException("Not supported yet.");
    }






}
